package com.pda.yuanjing.pages.xsck;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.JDBeanDNewOut;
import com.pda.yuanjing.beanout.JDBeanNewOut;
import com.pda.yuanjing.beanout.JDBillBeanOut;
import com.pda.yuanjing.pages.xsck.add.XSCKBillAddActivity;
import com.pda.yuanjing.pages.xsck.scanf.ScanfActivity;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.tools.SharedPreferencesManger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class XSCKBillActivity extends BaseActivity<CBillModel, CBillView, CBillPresent> implements CBillView {
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.login_id)
    EditText loginId;
    @BindView(R.id.loginbt)
    Button loginbt;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private String TAG = "XSCKBillActivity";
    JDBeanNewOut.DataBean dataBean = null;
    CBillAdapter cBillAdapter;
    List<JDBillBeanOut.DataBean> listdata = new ArrayList<>();
    List<JDBillBeanOut.DataBean> listdataTemp = new ArrayList<>();

    @Override
    public CBillModel createModel() {
        return new CBillModelImpl(this);
    }

    @Override
    public CBillView createView() {
        return this;
    }

    @Override
    public CBillPresent createPresenter() {
        return new CBillPresent();
    }

    @Override
    public void showlist(final List<JDBillBeanOut.DataBean> list) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                listdataTemp.clear();
                listdata = list;
                for (int i = 0; i < listdata.size(); i++) {
                    listdataTemp.add(listdata.get(i));
                }
                cBillAdapter = new CBillAdapter(XSCKBillActivity.this, listdataTemp);
                listview.setAdapter(cBillAdapter);
                loginId.setText("");
                refreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void goDeatail(JDBeanDNewOut dataBeann) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dataBeann.setJieDianName(dataBean.getJieDianName());
                dataBeann.setBatchID(dataBean.getBatchID());
                dataBeann.setProcessID(dataBean.getProcessID());
                intentToActivityWithSerializableParameter(XSCKBillActivity.this, ScanfActivity.class, PDApp.process,dataBeann);
                }
        });
    }

    @Override
    public void tip(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showMsg(str);
                loginId.setText("");
                refreshLayout.finishRefresh();
            }
        });
    }

    protected void initdata() {
        dataBean = (JDBeanNewOut.DataBean) getIntent().getSerializableExtra(PDApp.process);
        tvTitle.setText(dataBean.getJieDianName()+" "+ SharedPreferencesManger.getRealName());
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableHeaderTranslationContent(true);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
            }
        });
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cBillAdapter.setSelectedPosition(position);
                cBillAdapter.notifyDataSetInvalidated();
                dataBean.setBatchID(listdataTemp.get(position).getBatchID()+"");
                presenter.showDeatail(dataBean.getProcessID(),dataBean.getJieDianName(),dataBean.getBatchID());
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xsckbill);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.showlist(dataBean.getProcessID(),dataBean.getJieDianName(),dataBean.getBillType());
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @OnClick({R.id.iv_title_left, R.id.loginbt, R.id.iv_title_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.iv_title_right:
                intentToActivityWithSerializableParameter(this, XSCKBillAddActivity.class,PDApp.process,dataBean);
                break;
            case R.id.loginbt:
                listdataTemp.clear();
                if (loginId.getText().toString().length() == 0) {
                    listdataTemp.addAll(listdata);
                } else {
                    for (int i = 0; i < listdata.size(); i++) {
                        if (listdata.get(i).getBatch().contains(loginId.getText().toString())) {
                            listdataTemp.add(listdata.get(i));
                        }
                    }
                }
                cBillAdapter = new CBillAdapter(XSCKBillActivity.this, listdataTemp);
                listview.setAdapter(cBillAdapter);
                break;
        }
    }
}
